package org.biubiu0002.baiblog.core.controller;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.biubiu0002.baiblog.common.utils.PageUtils;
import org.biubiu0002.baiblog.common.utils.R;
import org.biubiu0002.baiblog.core.annotations.PermissionIgnore;
import org.biubiu0002.baiblog.core.model.vo.req.FollowReqVo;
import org.biubiu0002.baiblog.core.service.FriendshipService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: biubiu0002
 * @Date: 2022/8/1 10:57
 **/
@RestController
@RequestMapping("/friendship")
public class FriendshipController {

    @Resource
    private FriendshipService friendshipService;

    /**
     * 当前用户关注用户
     * 返回该用户粉丝数
     * @param followReqVo vo
     */
    @PutMapping("/follow")
    @PermissionIgnore
    public R follow(@RequestBody FollowReqVo followReqVo){
        Preconditions.checkArgument(StringUtils.isNotBlank(followReqVo.getUid()),"关注uid不能为空");
        Preconditions.checkArgument(followReqVo.getIsFollow()!=null,"follow不能为空");
        Preconditions.checkArgument(followReqVo.getIsFollow()==0||followReqVo.getIsFollow()==1,"follow只能为0或1");
        int fansCount = friendshipService.follow(followReqVo);
        return R.ok().put("data",fansCount);
    }


    /**
     * 当前用户是否关注该用户
     * 0:无关系 1:已关注 2:互相关注
     * @param uid 用户
     */
    @GetMapping("/isFollowed")
    @PermissionIgnore
    public R isFollowed(@RequestParam String uid){
        int type = friendshipService.isFollowed(uid);
        return R.ok().put("data",type);
    }

    /**
     * 用户的关注列表
     * @param page 页面
     * @param limit 页码
     * @return 分页结果
     */
    @GetMapping("/{uid}/follows")
    @PermissionIgnore
    public R follows(@PathVariable("uid")String uid, String page, String limit){
        int pageNum=1;
        int limitNum=15;
        if(StringUtils.isNotBlank(page)){
            pageNum=Math.max(1,Integer.parseInt(page));
        }
        if(StringUtils.isNotBlank(limit)){
            limitNum = Math.max(1,Integer.parseInt(limit));
        }
        PageUtils res = friendshipService.follows(uid, pageNum,limitNum);
        return R.ok().put("page",res);
    }

    /**
     * 用户的粉丝列表
     * @param page 页面
     * @param limit 页码
     * @return 分页结果
     */
    @GetMapping("/{uid}/fans")
    @PermissionIgnore
    public R fans(@PathVariable("uid")String uid, String page, String limit){
        int pageNum=1;
        int limitNum=15;
        if(StringUtils.isNotBlank(page)){
            pageNum=Math.max(1,Integer.parseInt(page));
        }
        if(StringUtils.isNotBlank(limit)){
            limitNum = Math.max(1,Integer.parseInt(limit));
        }
        PageUtils res = friendshipService.fans(uid, pageNum,limitNum);
        return R.ok().put("page",res);
    }

    /**
     * 用户的关注人数量
     * @param uid uid
     * @return 用户的关注人数量
     */
    @GetMapping("/{uid}/followCount")
    @PermissionIgnore
    public R followCount(@PathVariable("uid")String uid){
        int res = friendshipService.followCount(uid);
        return R.ok().put("data",res);
    }

    /**
     * 用户的粉丝数量
     * @param uid uid
     * @return 用户的粉丝数量
     */
    @GetMapping("/{uid}/fansCount")
    @PermissionIgnore
    public R fansCount(@PathVariable("uid")String uid){
        int res = friendshipService.fansCount(uid);
        return R.ok().put("data",res);
    }

}
